package com.itany.mall.action;

import java.util.List;
import java.util.Scanner;

import com.itany.mall.entity.Product;
import com.itany.mall.entity.ProductType;
import com.itany.mall.exception.ProductNameExistsException;
import com.itany.mall.exception.ProductTypeNameExistsException;
import com.itany.mall.exception.ProductTypeNotFoundException;
import com.itany.mall.exception.ServiceException;
import com.itany.mall.factory.Autowired;
import com.itany.mall.factory.ObjectFactory;
import com.itany.mall.service.ProductService;
import com.itany.mall.service.ProductTypeService;
import com.itany.mall.vo.PageInfo;

public class AdminAction {

	@Autowired("productTypeServiceProxy")
	private ProductTypeService productTypeService;

	@Autowired("productServiceProxy")
	private ProductService productService;

	{
		ObjectFactory.autowired(this);
	}

	public void menu() {
		do {
			System.out.println("1. 商品类型管理");
			System.out.println("2. 商品管理");
			System.out.println("3. 退出登录");
			System.out.println("请选择");
			switch (new Scanner(System.in).nextLine()) {
				case "1":
					productTypeMenu();
					break;
				case "2":
					productMenu();
					break;
				case "3":
					return;
				default:
					System.out.println("选择错误");
			}
		} while (true);
	}

	private void productMenu() {
		do {
			System.out.println("1. 查看所有商品");
			System.out.println("2. 添加商品");
			System.out.println("3. 返回上级");
			switch (new Scanner(System.in).nextLine()) {
				case "1":
					showAllProductPage();
					break;
				case "2":
					addProduct();
					break;
				case "3":
					return;
				default:
					System.out.println("选择错误");
			}
		} while (true);
	}

	// 分页查看所有商品
	private void showAllProductPage() {
		try {
			int pageNo = 1, pageSize = 5;
			do {
				PageInfo<Product> page = productService.findAll(pageNo, pageSize);
				/*
				 pageNo, 页码
				 pageSize, 每页显示条数
				 totalCount, 总数据条数
				 pageCount, 分多少页
				 List, 每页要显示的具体数据
				 */for (Product p : page.getList()) {
					System.out.print("商品编号: " + p.getId() + ", ");
					System.out.print("商品名称: " + p.getName() + ", ");
					System.out.print("商品单价: " + p.getPrice() + ", ");
					System.out.println("商品类型: " + p.getType().getName());
				}
				System.out.print("第一页(F)  ");
				System.out.print("上一页(P)  ");
				for (int i = 1; i <= page.getPageCount(); i++) {
					if (i == page.getPageNo()) {
						System.out.print("[" + i + "]  ");
						continue;
					}
					System.out.print(i + "  ");
				}
				System.out.print("下一页(N)  ");
				System.out.print("最后一页(L)  ");
				System.out.println("退出查询(x)");
				String str = new Scanner(System.in).nextLine();
				if (str != null && str.matches("[fFpPnNlLxX]|[\\d]+")) {
					// 对pageNo重新赋值, 并查询数据
					if ("f".equalsIgnoreCase(str)) {
						pageNo = 1;
					} else if ("p".equalsIgnoreCase(str)) {
						pageNo = page.getPreviousPageNo();
					} else if ("n".equalsIgnoreCase(str)) {
						pageNo = page.getNextPageNo();
					} else if ("l".equalsIgnoreCase(str)) {
						pageNo = page.getPageCount();
					} else if ("x".equalsIgnoreCase(str)) {
						return;
					} else {
						pageNo = Integer.parseInt(str);
						if (pageNo < 1) {
							pageNo = 1;
						} else if (pageNo > page.getPageCount()) {
							pageNo = page.getPageCount();
						}
					}
				}
			} while (true);

		} catch (ServiceException e) {
			System.out.println("服务器忙...");
		}
	}

	private void addProduct() {
		System.out.println("请输入商品名称");
		String name = new Scanner(System.in).nextLine();
		if (name == null || name.trim().isEmpty()) {
			System.out.println("商品名称不可为空");
			return;

		}
		name = name.trim();

		System.out.println("请输入商品单价");
		String priceStr = new Scanner(System.in).nextLine();
		if (priceStr == null || !priceStr.matches("[\\d]+([\\.][\\d]{1,2})?")) {
			System.out.println("请输入有效价格");
			return;
		}
		double price = Double.parseDouble(priceStr);

		System.out.println("请选择类型");
		showAllProductTypes();
		String typeIdStr = new Scanner(System.in).nextLine();
		if (typeIdStr == null || !typeIdStr.matches("[\\d]+")) {
			System.out.println("请输入有效类型编号");
			return;
		}
		long typeId = Long.parseLong(typeIdStr);

		try {
			productService.addProduct(name, price, typeId);
			System.out.println("添加成功");
		} catch (ProductNameExistsException e) {
			System.out.println("商品名称已存在");
		} catch (ProductTypeNotFoundException e) {
			System.out.println("所选商品类型不存在");
		} catch (ServiceException e) {
			System.out.println("服务器忙...");
		}

	}

	private void showAllProduct() {
		try {
			List<Product> list = productService.findAll();
			for (Product p : list) {
				System.out.print("商品编号: " + p.getId() + ", ");
				System.out.print("商品名称: " + p.getName() + ", ");
				System.out.print("商品单价: " + p.getPrice() + ", ");
				System.out.println("商品类型: " + p.getType().getName());
			}
		} catch (ServiceException e) {
			System.out.println("服务器忙...");
		}
	}

	private void productTypeMenu() {
		do {
			System.out.println("1. 查看所有商品类型");
			System.out.println("2. 添加商品类型");
			System.out.println("3. 返回上级");
			switch (new Scanner(System.in).nextLine()) {
				case "1":
					showAllProductTypes();
					break;
				case "2":
					addProductType();
					break;
				case "3":
					return;
				default:
					System.out.println("选择错误");
			}
		} while (true);
	}

	// 添加商品类型
	private void addProductType() {
		System.out.println("请输入商品类型名称");
		String name = new Scanner(System.in).nextLine();
		if (name == null || name.trim().isEmpty()) {
			System.out.println("商品类型名称不可为空");
			return;
		}
		try {
			productTypeService.addProductType(name.trim());
			System.out.println("添加成功");
		} catch (ProductTypeNameExistsException e) {
			System.out.println("商品类型名称已存在");
		} catch (ServiceException e) {
			System.out.println("服务器忙...");
		}
	}

	// 查看所有商品类型
	private void showAllProductTypes() {
		try {
			List<ProductType> list = productTypeService.findAll();
			for (ProductType pt : list) {
				System.out.print("类型编号: " + pt.getId() + ", ");
				System.out.println("类型名称: " + pt.getName());
			}
		} catch (ServiceException e) {
			System.out.println("服务器忙...");
		}
	}

}
